<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="供应商编号" prop="supplierNumber">
        <el-input v-model="formData.supplierNumber" placeholder="请输入供应商编号" />
      </el-form-item>
      <el-form-item label="供应商简称" prop="supplierNickName">
        <el-input v-model="formData.supplierNickName" placeholder="请输入供应商简称" />
      </el-form-item>
      <el-form-item label="供应商全称" prop="supplierFullName">
        <el-input v-model="formData.supplierFullName" placeholder="请输入供应商全称" />
      </el-form-item>
      <el-form-item label="电话" prop="supplierPhone">
        <el-input v-model="formData.supplierPhone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="传真" prop="supplierFax">
        <el-input v-model="formData.supplierFax" placeholder="请输入传真" />
      </el-form-item>
      <el-form-item label="Email" prop="supplierEmailAddress">
        <el-input v-model="formData.supplierEmailAddress" placeholder="请输入Email" />
      </el-form-item>
      <el-form-item label="供应商地址" prop="supplierAddress">
        <el-input v-model="formData.supplierAddress" placeholder="请输入供应商地址" />
      </el-form-item>
      <el-form-item label="供应商备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入供应商备注" />
      </el-form-item>
      <el-form-item label="供应商类型" prop="supplierType">
        <el-select v-model="formData.supplierType" placeholder="请选择供应商类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SUPPLIER_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商入库类型" prop="supplierStockedType">
        <el-select v-model="formData.supplierStockedType" placeholder="请选择供应商入库类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SUPPLIER_STOCKED_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商性质" prop="supplierNature">
        <el-select v-model="formData.supplierNature" placeholder="请选择供应商性质">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASE_CUSTOMER_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="纸板类型" prop="cardboardType">
        <el-select v-model="formData.cardboardType" placeholder="请选择纸板类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PAPER_CARD_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="supplierContacts">
        <el-input v-model="formData.supplierContacts" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="供应商交货天数" prop="deliveryDays">
        <el-input v-model="formData.deliveryDays" placeholder="请输入供应商交货天数" />
      </el-form-item>
      <el-form-item label="供应商单位" prop="boardcardUnit">
        <el-select v-model="formData.boardcardUnit" placeholder="请选择供应商单位">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.VALUATION_UNIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金额计算方式" prop="calculatePriceFormula">
        <el-select v-model="formData.calculatePriceFormula" placeholder="请选择金额计算方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.VALUATION_FORMULA)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总重量取舍" prop="weightRoundingMode">
        <el-select v-model="formData.weightRoundingMode" placeholder="请选择总重量取舍">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ERP_DECIMAL_ROUNDING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总面积取舍" prop="areaRoundingMode">
        <el-select v-model="formData.areaRoundingMode" placeholder="请选择总面积取舍">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ERP_DECIMAL_ROUNDING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总体积取舍" prop="volumeRoundingMode">
        <el-select v-model="formData.volumeRoundingMode" placeholder="请选择总体积取舍">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ERP_DECIMAL_ROUNDING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总重量位数" prop="weightDecimalPlaces">
        <el-select v-model="formData.weightDecimalPlaces" placeholder="请选择总重量位数">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DECIMAL_ROUND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总面积位数" prop="areaDecimalPlaces">
        <el-select v-model="formData.areaDecimalPlaces" placeholder="请选择总面积位数">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DECIMAL_ROUND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总体积位数" prop="volumeRoundingPlaces">
        <el-select v-model="formData.volumeRoundingPlaces" placeholder="请选择总体积位数">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DECIMAL_ROUND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单价保留小数位" prop="unitPriceDecimalPlaces">
        <el-select v-model="formData.unitPriceDecimalPlaces" placeholder="请选择单价保留小数位">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DECIMAL_ROUND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金额保留小数位" prop="priceDecimalPlaces">
        <el-select v-model="formData.priceDecimalPlaces" placeholder="请选择金额保留小数位">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DECIMAL_ROUND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单价小数位截取方式" prop="unitPriceRoundingMode">
        <el-select v-model="formData.unitPriceRoundingMode" placeholder="请选择单价小数位截取方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.UNIT_PRICE_ROUNDING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金额小数位截取方式" prop="priceRoundingMode">
        <el-select v-model="formData.priceRoundingMode" placeholder="请选择金额小数位截取方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.UNIT_PRICE_ROUNDING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="期初欠款" prop="initialAmount">
        <el-input v-model="formData.initialAmount" placeholder="请输入期初欠款" />
      </el-form-item>
      <el-form-item label="付款方式" prop="payMode">
        <el-input v-model="formData.payMode" placeholder="请输入付款方式" />
      </el-form-item>
      <el-form-item label="对公账号" prop="bankAccountNumber">
        <el-input v-model="formData.bankAccountNumber" placeholder="请输入对公账号" />
      </el-form-item>
      <el-form-item label="备用账号1" prop="bankAccountNumber1">
        <el-input v-model="formData.bankAccountNumber1" placeholder="请输入备用账号1" />
      </el-form-item>
      <el-form-item label="备用账号2" prop="bankAccountNumber2">
        <el-input v-model="formData.bankAccountNumber2" placeholder="请输入备用账号2" />
      </el-form-item>
      <el-form-item label="开户行" prop="bankOfDeposit">
        <el-input v-model="formData.bankOfDeposit" placeholder="请输入开户行" />
      </el-form-item>
      <el-form-item label="开户行地址" prop="bankOfDepositAddress">
        <el-input v-model="formData.bankOfDepositAddress" placeholder="请输入开户行地址" />
      </el-form-item>
      <el-form-item label="税号" prop="taxNumber">
        <el-input v-model="formData.taxNumber" placeholder="请输入税号" />
      </el-form-item>
      <el-form-item label="付款周期" prop="paymentCycle">
        <el-input v-model="formData.paymentCycle" placeholder="请输入付款周期" />
      </el-form-item>
      <el-form-item label="币别" prop="currency">
        <el-select v-model="formData.currency" placeholder="请选择币别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CURRENCY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="月结方式" prop="monthSettlementMode">
        <el-input v-model="formData.monthSettlementMode" placeholder="请输入月结方式" />
      </el-form-item>
      <el-form-item label="报价税率" prop="quoteTaxRate">
        <el-input v-model="formData.quoteTaxRate" placeholder="请输入报价税率" />
      </el-form-item>
      <el-form-item label="税别" prop="taxType">
        <el-select v-model="formData.taxType" placeholder="请选择税别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.TAX_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="折扣" prop="discount">
        <el-input v-model="formData.discount" placeholder="请输入折扣" />
      </el-form-item>
      <el-form-item label="对账日" prop="reconcileDay">
        <el-input v-model="formData.reconcileDay" placeholder="请输入对账日" />
      </el-form-item>
      <el-form-item label="会计凭证代码" prop="accountingVoucherCode">
        <el-select v-model="formData.accountingVoucherCode" placeholder="请选择会计凭证代码">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ACCOUNTING_VOUCHER_CODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="备注" name="baseSupplierRemark">
        <BaseSupplierRemarkForm ref="baseSupplierRemarkFormRef" :supplier-id="formData.id" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { BaseSupplierApi, BaseSupplierVO } from '@/api/erp/basesupplier'
import BaseSupplierRemarkForm from './components/BaseSupplierRemarkForm.vue'

/** ERP基础资料-供应商资料 表单 */
defineOptions({ name: 'BaseSupplierForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  supplierNumber: undefined,
  supplierNickName: undefined,
  supplierFullName: undefined,
  supplierPhone: undefined,
  supplierFax: undefined,
  supplierEmailAddress: undefined,
  supplierAddress: undefined,
  remark: undefined,
  supplierType: undefined,
  supplierStockedType: undefined,
  supplierNature: undefined,
  cardboardType: undefined,
  supplierContacts: undefined,
  deliveryDays: undefined,
  boardcardUnit: undefined,
  calculatePriceFormula: undefined,
  weightRoundingMode: undefined,
  areaRoundingMode: undefined,
  volumeRoundingMode: undefined,
  weightDecimalPlaces: undefined,
  areaDecimalPlaces: undefined,
  volumeRoundingPlaces: undefined,
  unitPriceDecimalPlaces: undefined,
  priceDecimalPlaces: undefined,
  unitPriceRoundingMode: undefined,
  priceRoundingMode: undefined,
  initialAmount: undefined,
  payMode: undefined,
  bankAccountNumber: undefined,
  bankAccountNumber1: undefined,
  bankAccountNumber2: undefined,
  bankOfDeposit: undefined,
  bankOfDepositAddress: undefined,
  taxNumber: undefined,
  paymentCycle: undefined,
  currency: undefined,
  monthSettlementMode: undefined,
  quoteTaxRate: undefined,
  taxType: undefined,
  discount: undefined,
  reconcileDay: undefined,
  accountingVoucherCode: undefined
})
const formRules = reactive({
  supplierNumber: [{ required: true, message: '供应商编号不能为空', trigger: 'blur' }],
  supplierNickName: [{ required: true, message: '供应商简称不能为空', trigger: 'blur' }],
  deliveryDays: [{ required: true, message: '供应商交货天数不能为空', trigger: 'blur' }],
  boardcardUnit: [{ required: true, message: '供应商单位不能为空', trigger: 'change' }],
  weightDecimalPlaces: [{ required: true, message: '总重量位数不能为空', trigger: 'change' }],
  areaDecimalPlaces: [{ required: true, message: '总面积位数不能为空', trigger: 'change' }],
  volumeRoundingPlaces: [{ required: true, message: '总体积位数不能为空', trigger: 'change' }],
  unitPriceDecimalPlaces: [{ required: true, message: '单价保留小数位不能为空', trigger: 'change' }],
  priceDecimalPlaces: [{ required: true, message: '金额保留小数位不能为空', trigger: 'change' }],
  unitPriceRoundingMode: [{ required: true, message: '单价小数位截取方式不能为空', trigger: 'change' }],
  priceRoundingMode: [{ required: true, message: '金额小数位截取方式不能为空', trigger: 'change' }],
  initialAmount: [{ required: true, message: '期初欠款不能为空', trigger: 'blur' }],
  paymentCycle: [{ required: true, message: '付款周期不能为空', trigger: 'blur' }],
  quoteTaxRate: [{ required: true, message: '报价税率不能为空', trigger: 'blur' }],
  discount: [{ required: true, message: '折扣不能为空', trigger: 'blur' }],
  reconcileDay: [{ required: true, message: '对账日不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('baseSupplierRemark')
const baseSupplierRemarkFormRef = ref()

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseSupplierApi.getBaseSupplier(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await baseSupplierRemarkFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'baseSupplierRemark'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BaseSupplierVO
    // 拼接子表的数据
    data.baseSupplierRemarks = baseSupplierRemarkFormRef.value.getData()
    if (formType.value === 'create') {
      await BaseSupplierApi.createBaseSupplier(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseSupplierApi.updateBaseSupplier(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    supplierNumber: undefined,
    supplierNickName: undefined,
    supplierFullName: undefined,
    supplierPhone: undefined,
    supplierFax: undefined,
    supplierEmailAddress: undefined,
    supplierAddress: undefined,
    remark: undefined,
    supplierType: undefined,
    supplierStockedType: undefined,
    supplierNature: undefined,
    cardboardType: undefined,
    supplierContacts: undefined,
    deliveryDays: undefined,
    boardcardUnit: undefined,
    calculatePriceFormula: undefined,
    weightRoundingMode: undefined,
    areaRoundingMode: undefined,
    volumeRoundingMode: undefined,
    weightDecimalPlaces: undefined,
    areaDecimalPlaces: undefined,
    volumeRoundingPlaces: undefined,
    unitPriceDecimalPlaces: undefined,
    priceDecimalPlaces: undefined,
    unitPriceRoundingMode: undefined,
    priceRoundingMode: undefined,
    initialAmount: undefined,
    payMode: undefined,
    bankAccountNumber: undefined,
    bankAccountNumber1: undefined,
    bankAccountNumber2: undefined,
    bankOfDeposit: undefined,
    bankOfDepositAddress: undefined,
    taxNumber: undefined,
    paymentCycle: undefined,
    currency: undefined,
    monthSettlementMode: undefined,
    quoteTaxRate: undefined,
    taxType: undefined,
    discount: undefined,
    reconcileDay: undefined,
    accountingVoucherCode: undefined
  }
  formRef.value?.resetFields()
}
</script>
